package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;

import negocio.DisponibilidadInstructor;
import negocio.DisponibilidadInstructorControlador;
import negocio.Sala;
import negocio.Usuario;
import lu.tudor.santec.jtimechooser.JTimeChooser;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Time;
import java.util.ArrayList;

public class InstructoresDisponiblidad extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private Instructores padre;
	private JPanel panel;
	private JPanel panel_1;
	private JTextField textField_1;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private Usuario instructor;
	private ArrayList<DisponibilidadInstructor> lista = new ArrayList<DisponibilidadInstructor>();
	JCheckBox checkBox;
	JCheckBox checkBox_1;
	JCheckBox checkBox_2;
	JCheckBox checkBox_3;
	JCheckBox checkBox_4;
	JCheckBox checkBox_5;
	JCheckBox checkBox_6;
	JTimeChooser timeChooser;
	JTimeChooser timeChooser_1;
	JTimeChooser timeChooser_2;
	JTimeChooser timeChooser_3;
	JTimeChooser timeChooser_4;
	JTimeChooser timeChooser_5;
	JTimeChooser timeChooser_6;
	JTimeChooser timeChooser_7;
	JTimeChooser timeChooser_8;
	JTimeChooser timeChooser_9;
	JTimeChooser timeChooser_10;
	JTimeChooser timeChooser_11;
	JTimeChooser timeChooser_12;
	JTimeChooser timeChooser_13;
	
	public void llenarDatos()
	{
		this.lista=DisponibilidadInstructorControlador.getDisponibilidadPorInstructor(instructor);
		for(DisponibilidadInstructor i:lista)
		{
			if(i.getDiaSemana().equals("lunes"))
			{
				this.checkBox.setSelected(true);
				timeChooser.setTime(i.getHoraInicio());
				timeChooser_1.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("martes"))
			{
				this.checkBox_1.setSelected(true);
				timeChooser_2.setTime(i.getHoraInicio());
				timeChooser_3.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("miercoles"))
			{
				this.checkBox_2.setSelected(true);
				timeChooser_4.setTime(i.getHoraInicio());
				timeChooser_5.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("jueves"))
			{
				this.checkBox_3.setSelected(true);
				timeChooser_6.setTime(i.getHoraInicio());
				timeChooser_7.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("viernes"))
			{
				this.checkBox_4.setSelected(true);
				timeChooser_8.setTime(i.getHoraInicio());
				timeChooser_9.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("sabado"))
			{
				this.checkBox_5.setSelected(true);
				timeChooser_10.setTime(i.getHoraInicio());
				timeChooser_11.setTime(i.getHoraFin());
			}
			
			if(i.getDiaSemana().equals("domingo"))
			{
				this.checkBox_6.setSelected(true);
				timeChooser_12.setTime(i.getHoraInicio());
				timeChooser_13.setTime(i.getHoraFin());
			}
		}
	}
	/**
	 * Launch the application.
	 
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InstructoresDisponiblidad frame = new InstructoresDisponiblidad();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	 */
	
	
	/**
	 * Create the frame.
	 * @param instructor 
	 * @param Padre 
	 */
	public InstructoresDisponiblidad(Usuario instructor, Instructores padre) {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				padre.nullInstructoresDisponiblidad();
				dispose();
			}
		});
		
		this.padre = padre;
		this.instructor=instructor;
		setTitle("Disponibilidad");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 856, 336);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "Dias y horas", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(12, 84, 809, 132);
		contentPane.add(panel);
		
		panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(88, 46, 97, 81);
		panel.add(panel_1);
		
		timeChooser = new JTimeChooser();
		timeChooser.setBounds(10, 11, 50, 20);
		panel_1.add(timeChooser);
		
		timeChooser_1 = new JTimeChooser();
		timeChooser_1.setBounds(10, 42, 50, 20);
		panel_1.add(timeChooser_1);
		
		checkBox = new JCheckBox("Lunes");
		checkBox.setBounds(107, 16, 78, 23);
		panel.add(checkBox);
		
		checkBox_1 = new JCheckBox("Martes");
		checkBox_1.setBounds(203, 16, 85, 23);
		panel.add(checkBox_1);
		
		checkBox_2 = new JCheckBox("Miercoles");
		checkBox_2.setBounds(308, 16, 83, 23);
		panel.add(checkBox_2);
		
		checkBox_3 = new JCheckBox("Jueves");
		checkBox_3.setBounds(414, 16, 80, 23);
		panel.add(checkBox_3);
		
		checkBox_4 = new JCheckBox("Viernes");
		checkBox_4.setBounds(518, 16, 79, 23);
		panel.add(checkBox_4);
		
		checkBox_5 = new JCheckBox("Sabado");
		checkBox_5.setBounds(619, 16, 81, 23);
		panel.add(checkBox_5);
		
		checkBox_6 = new JCheckBox("Domingo");
		checkBox_6.setBounds(720, 16, 83, 23);
		panel.add(checkBox_6);
		
		JLabel label = new JLabel("Hora inicio");
		label.setBounds(6, 70, 72, 14);
		panel.add(label);
		
		JLabel label_1 = new JLabel("Hora fin");
		label_1.setBounds(6, 95, 46, 14);
		panel.add(label_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(191, 46, 97, 81);
		panel.add(panel_2);
		
		timeChooser_2 = new JTimeChooser();
		timeChooser_2.setBounds(10, 11, 50, 20);
		panel_2.add(timeChooser_2);
		
		timeChooser_3 = new JTimeChooser();
		timeChooser_3.setBounds(10, 42, 50, 20);
		panel_2.add(timeChooser_3);
		
		JPanel panel_3 = new JPanel();
		panel_3.setLayout(null);
		panel_3.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setBounds(294, 46, 97, 81);
		panel.add(panel_3);
		
		timeChooser_4 = new JTimeChooser();
		timeChooser_4.setBounds(10, 11, 50, 20);
		panel_3.add(timeChooser_4);
		
		timeChooser_5 = new JTimeChooser();
		timeChooser_5.setBounds(10, 42, 50, 20);
		panel_3.add(timeChooser_5);
		
		JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_4.setBounds(397, 46, 97, 81);
		panel.add(panel_4);
		
		timeChooser_6 = new JTimeChooser();
		timeChooser_6.setBounds(10, 11, 50, 20);
		panel_4.add(timeChooser_6);
		
		timeChooser_7 = new JTimeChooser();
		timeChooser_7.setBounds(10, 40, 50, 20);
		panel_4.add(timeChooser_7);
		
		JPanel panel_5 = new JPanel();
		panel_5.setLayout(null);
		panel_5.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_5.setBounds(500, 46, 97, 81);
		panel.add(panel_5);
		
		timeChooser_8 = new JTimeChooser();
		timeChooser_8.setBounds(10, 11, 50, 20);
		panel_5.add(timeChooser_8);
		
		timeChooser_9 = new JTimeChooser();
		timeChooser_9.setBounds(10, 42, 50, 20);
		panel_5.add(timeChooser_9);
		
		JPanel panel_6 = new JPanel();
		panel_6.setLayout(null);
		panel_6.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_6.setBounds(603, 46, 97, 81);
		panel.add(panel_6);
		
		timeChooser_10 = new JTimeChooser();
		timeChooser_10.setBounds(10, 11, 50, 20);
		panel_6.add(timeChooser_10);
		
		timeChooser_11 = new JTimeChooser();
		timeChooser_11.setBounds(10, 42, 50, 20);
		panel_6.add(timeChooser_11);
		
		JPanel panel_7 = new JPanel();
		panel_7.setLayout(null);
		panel_7.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_7.setBounds(706, 46, 97, 81);
		panel.add(panel_7);
		
		timeChooser_12 = new JTimeChooser();
		timeChooser_12.setBounds(10, 11, 50, 20);
		panel_7.add(timeChooser_12);
		
		timeChooser_13 = new JTimeChooser();
		timeChooser_13.setBounds(10, 42, 50, 20);
		panel_7.add(timeChooser_13);
		
		JLabel lblInstructor = new JLabel("Instructor:");
		lblInstructor.setBounds(12, 23, 89, 14);
		contentPane.add(lblInstructor);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(113, 20, 159, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		textField.setText(instructor.getNombre() +" "+ instructor.getApellido());
		
		JLabel lblDni = new JLabel("Dni:");
		lblDni.setBounds(12, 48, 53, 14);
		contentPane.add(lblDni);
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(113, 45, 159, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		textField_1.setText(instructor.getDni());
		
		btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(checkBox.isSelected())
				{
					Time horaInicioLunes= new Time(timeChooser.getHours(), timeChooser.getMinutes(), timeChooser.getSeconds());
					Time horaFinLunes=new Time (timeChooser_1.getHours(), timeChooser_1.getMinutes(), timeChooser_1.getSeconds());
					DisponibilidadInstructor lunes=new DisponibilidadInstructor(instructor, "lunes", horaInicioLunes, horaFinLunes);
					DisponibilidadInstructorControlador.guardar(lunes);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "lunes");
				
				if(checkBox_1.isSelected())
				{
					Time horaInicioMartes= new Time(timeChooser_2.getHours(), timeChooser_2.getMinutes(), timeChooser_2.getSeconds());
					Time horaFinMartes= new Time(timeChooser_3.getHours(), timeChooser_3.getMinutes(), timeChooser_3.getSeconds());
					DisponibilidadInstructor martes=new DisponibilidadInstructor(instructor, "martes", horaInicioMartes, horaFinMartes);
					DisponibilidadInstructorControlador.guardar(martes);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "martes");
				
				if(checkBox_2.isSelected())
				{
					Time horaInicioMiercoles= new Time(timeChooser_4.getHours(), timeChooser_4.getMinutes(), timeChooser_4.getSeconds());
					Time horaFinMiercoles= new Time(timeChooser_5.getHours(), timeChooser_5.getMinutes(), timeChooser_5.getSeconds());
					DisponibilidadInstructor miercoles=new DisponibilidadInstructor(instructor, "miercoles", horaInicioMiercoles, horaFinMiercoles);
					DisponibilidadInstructorControlador.guardar(miercoles);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "miercoles");
				
				if(checkBox_3.isSelected())
				{
					Time horaInicioJueves= new Time(timeChooser_6.getHours(), timeChooser_6.getMinutes(), timeChooser_6.getSeconds());
					Time horaFinJueves= new Time(timeChooser_7.getHours(), timeChooser_7.getMinutes(), timeChooser_7.getSeconds());
					DisponibilidadInstructor jueves=new DisponibilidadInstructor(instructor, "jueves", horaInicioJueves, horaFinJueves);
					DisponibilidadInstructorControlador.guardar(jueves);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "jueves");
				
				if(checkBox_4.isSelected())
				{
					Time horaInicioViernes= new Time(timeChooser_8.getHours(), timeChooser_8.getMinutes(), timeChooser_8.getSeconds());
					Time horaFinViernes= new Time(timeChooser_9.getHours(), timeChooser_9.getMinutes(), timeChooser_9.getSeconds());
					DisponibilidadInstructor viernes=new DisponibilidadInstructor(instructor, "viernes", horaInicioViernes, horaFinViernes);
					DisponibilidadInstructorControlador.guardar(viernes);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "viernes");
				
				if(checkBox_5.isSelected())
				{
					Time horaInicioSabado= new Time(timeChooser_10.getHours(), timeChooser_10.getMinutes(), timeChooser_10.getSeconds());
					Time horaFinSabado= new Time(timeChooser_11.getHours(), timeChooser_11.getMinutes(), timeChooser_11.getSeconds());
					DisponibilidadInstructor sabado=new DisponibilidadInstructor(instructor, "sabado", horaInicioSabado, horaFinSabado);
					DisponibilidadInstructorControlador.guardar(sabado);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "sabado");
				
				if(checkBox_6.isSelected())
				{
					Time horaInicioDomingo= new Time(timeChooser_12.getHours(), timeChooser_12.getMinutes(), timeChooser_12.getSeconds());
					Time horaFinDomingo= new Time(timeChooser_13.getHours(), timeChooser_13.getMinutes(), timeChooser_13.getSeconds());
					DisponibilidadInstructor domingo=new DisponibilidadInstructor(instructor, "domingo", horaInicioDomingo, horaFinDomingo);
					DisponibilidadInstructorControlador.guardar(domingo);
				}
				else
					DisponibilidadInstructorControlador.borrar(instructor, "lunes");
				dispose();
			}
		});
		btnAceptar.setBounds(633, 264, 89, 23);
		contentPane.add(btnAceptar);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				padre.nullInstructoresDisponiblidad();
				dispose();
			}
		});
		btnCancelar.setBounds(732, 264, 89, 23);
		contentPane.add(btnCancelar);
		this.llenarDatos();
	}
}
